package com.ac.exception;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ac.model.LogSystem;
import com.ac.service.LogSystemService;

import ch.qos.logback.classic.Logger;
import lombok.AllArgsConstructor;

/**
 * 异常业务管理
 */
@Component
@AllArgsConstructor
public class ExceptionService {

	public static final Logger logger = (Logger) LoggerFactory.getLogger(ExceptionService.class);

	final LogSystemService LogSystemService;

	public static ExceptionService m;

	@PostConstruct
	public void init() {
		m = this;
	}

	public static String exceptionToString(Exception e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		e.printStackTrace(pw);

		logger.error("异常信息：", e);

		return sw.toString();
	}

	public static void addLogSystem(LogSystem logSystem) {
		m.LogSystemService.insert(logSystem);
	}

	/**
	 * 获取请求地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getApiUrl(HttpServletRequest request) {
		String url = request.getRequestURL().toString();
		url = url.split("//")[1];
		url = url.substring(url.indexOf("/"));
		return url;
	}

}
